package com.itheima;

import java.util.HashMap;
import java.util.Map;

public class MapPractice {
    public static void main(String[] args) {
        Map<String, String> data = new HashMap<String, String>();
        data.put("wang1","A");
        data.put("wang2","B");
        data.put("wang3","C");
        data.put("wang4","D");
        data.put("li5","A");
        data.put("li6","B");
        data.put("li7","C");
        data.put("li8","D");
        data.put("zhao1","A");
        data.put("zhao2","B");
        data.put("zhao3","C");

        //调用方法统计
        count(data);
    }
    public static void count(Map<String, String> data) {
        //1.创建一个Map集合，用来存储统计结果
        //2.遍历data集合（key--value）

        //6.遍历新的map集合，输出结果
        Map<String, Integer> resuitMap = new HashMap<>();

        data.forEach((k,v)->{
            //---3.将value（A）拿出来，将value（A）当作key存储到新的集合中，
            //---4.若A存在，那么将A对应的value+1
            //---5.若A不存在，那么将A---1存储到新的map集合中
              //判断v是否已经在resultMap中存在
            boolean containsKey = resuitMap.containsKey(v);
            if(containsKey){
                //v已经存在，将老的值取出来，+1，重新放回
                Integer oldCount = resuitMap.get(v);
                resuitMap.put(v, oldCount+1);
            }else {
                resuitMap.put(v, 1);
            }

        });
    }
}
